package com.alipay.app.groupmeal.standard.ui.c.pre;

import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.base.BasePresentation;
import com.alipay.app.groupmeal.standard.bean.AliPayResVo;
import com.alipay.app.groupmeal.standard.http.bean.resp.QrPayModel;
import com.alipay.app.groupmeal.standard.http.bean.resp.Result;
import com.alipay.app.groupmeal.standard.offline.bean.FkBean;
import com.alipay.app.groupmeal.standard.ui.b.util.ScanKeyManager;
import com.alipay.app.groupmeal.standard.ui.b.util.WIth;
import com.alipay.app.groupmeal.standard.ui.c.SelectPayActivity;
import com.alipay.app.groupmeal.standard.ui.c.abcp.CPayingAbcpActivity2;
import com.alipay.app.groupmeal.standard.util.ByteUtil;
import com.alipay.app.groupmeal.standard.util.USBTransferUtil;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.internal.observers.BlockingBaseObserver;

public class PayingMainPre extends BasePresentation {

    private ScanKeyManager scanKeyManager;

    private ProgressBar progressLoading_b;

    private CPayingAbcpActivity2 mActivity;

    private ConstraintLayout loadingCon;

    private ImageView imageView;

    private LinearLayout linear;

    private LinearLayout errLinear;

    private LinearLayout successLinear;

    private TextView tvLoading_b;

    private TextView tv_close_time;


    private TextView cardNoTv;
    private TextView nameTv;
    private TextView btyeTv;
    private TextView kyeTv;

    private String amount;

    private ScanQrMainPre.NetRes netRes;

    private String qrCode;

    public void setNetRes(ScanQrMainPre.NetRes netRes) {
        this.netRes = netRes;
    }

    public PayingMainPre(CPayingAbcpActivity2 outerContext, Display display) {
        super(outerContext, display);
        mActivity = outerContext;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        } else {
            getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
        this.amount = "0";

    }

    public void toPay(String qrCode) {
        this.qrCode = qrCode;
        if (qrCode.length() >= 18 && "1".equals(qrCode.substring(0, 1))) {
            progressLoading_b.setIndeterminateDrawable(this.getResources().getDrawable(R.drawable.progress_loading_wechat));
        }
        //开始发起支付

    }

    @Override
    protected int getLayoutId() {
//        if (ConfigConstant.displayType == BaseConstant.DISPLAY_TYPE.T2) {
//            return R.layout.activity_scan_qr_ft2;
//        } else {
//            return R.layout.activity_scan_qr;
//        }
        return R.layout.activity_paying;
    }

    @Override
    protected void initView() {
        loadingCon = findViewById(R.id.loadingCon);
        progressLoading_b = findViewById(R.id.progressLoading_b);
        imageView = findViewById(R.id.iconImg);
        linear = findViewById(R.id.linear);
        tvLoading_b = findViewById(R.id.tvLoading_b);
        tv_close_time = findViewById(R.id.tv_close_time);
        errLinear = findViewById(R.id.errLinear);
        successLinear = findViewById(R.id.successLinear);
        cardNoTv = findViewById(R.id.cardNoTv);
        nameTv = findViewById(R.id.nameTv);
        btyeTv = findViewById(R.id.btyeTv);
        kyeTv = findViewById(R.id.kyeTv);
    }

//    String order;

    @Override
    public boolean onKeyEventBack(KeyEvent event) {
        mActivity.onKeyEventBack(event);
        return super.onKeyEventBack(event);
    }


    public void setPayingMsg(String msg) {
        tvLoading_b.setText(msg);
    }


    public void showSuccess(FkBean fkBean) {
        loadingCon.setVisibility(View.GONE);
        successLinear.setVisibility(View.VISIBLE);
        if (fkBean != null) {
            cardNoTv.setText("卡号：" + fkBean.getCardno());
            nameTv.setText("姓名：" + fkBean.getName());
            btyeTv.setText("补贴余额：" + fkBean.getBtye());
            kyeTv.setText("卡余额：" + fkBean.getKye());
        }
        if (USBTransferUtil.isOpen){
            byte[] to_send = ByteUtil.hexStringToByte("A00106A7");
            int rel = USBTransferUtil.driver.WriteData(to_send, to_send.length);
            if (rel < 0) {
                USBTransferUtil.isOpen = false;
                USBTransferUtil.driver.CloseDevice();
            }
        }
//
    }

    public void showError() {
        loadingCon.setVisibility(View.GONE);
        errLinear.setVisibility(View.VISIBLE);
    }


    @Override
    protected void initData() {

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE) {
            mActivity.dispatchKeyEvent(event);
        } else if (event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
//            scanKeyManager.analysisKeyEvent(event);
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    public TextView getTvCloseTime() {
        return tv_close_time;
    }

    public void setTvCloseTime(TextView tv_close_time) {
        this.tv_close_time = tv_close_time;
    }

    public interface NetRes {
        void netRes(Result<AliPayResVo> aliPayResVoResult, Throwable e);
    }
}